package com.myframe.www.view.recyclerview;

import android.content.Context;
import android.support.annotation.CallSuper;

import com.myframe.www.main.adapter.MeAdapter;
import com.myframe.www.view.listener.OnLoadMoreListener;
import com.myframe.www.view.listener.OnPageRefreshListener;
import com.myframe.www.view.status.StatusView;
import com.myframe.www.view.status.ViewConstant;
import com.yong.android.framework.view.recyclerview.CellListAdapter;

/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

public class DefaultRecyclerViewAdapter extends CellListAdapter {
    private DefaultStatusCell mStatusCell;
    private DefaultLoadMoreCell mLoadMoreCell;

    public DefaultRecyclerViewAdapter(Context context) {
        super(context);
        mStatusCell = new DefaultStatusCell();
        setStatusCell(mStatusCell);
        mStatusCell.setEnable(true);
    }

    /**
     * 页面刷新
     */
    public void notifyPageRefresh() {
        if (getTotalCount() == 0) {
            mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_REFRESH);
            notifyDataSetChanged();
        }
    }

    /**
     * 页面刷新监听事件
     *
     * @param onPageRefreshListener
     */
    public void setOnPageRefreshListener(OnPageRefreshListener onPageRefreshListener) {
        mStatusCell.setOnPageRefreshListener(onPageRefreshListener);
    }


    /**
     * 加载错误页面视图
     */
    @CallSuper
    public void notifyFailure() {
        if (getTotalCount() == 0) {
            if (mLoadMoreCell != null) {
                mLoadMoreCell.setEnable(false);
                mLoadMoreCell.init();
            }
            mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_NETWORK_FAILURE);
            notifyDataSetChanged();
        }
    }

    /**
     * 加载更多失败
     */
    @CallSuper
    public void notifyLoadMoreFailure() {
        if (isLoadMoreEnable()) {
            mLoadMoreCell.notifyFailure();
        }
    }

    /**
     * 加载成功
     */
    public final void notifySuccess() {
        if (mLoadMoreCell != null) {
            if (getPage() == DefaultLoadMoreCell.INIT_PAGE && getTotalCount() == 0) {
                mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_EMPTY);
                if (mLoadMoreCell != null) {
                    mLoadMoreCell.setEnable(false);
                }
                notifyDataSetChanged();
            } else {
                mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_SUCCESS);
                mLoadMoreCell.notifySuccess();
            }
        } else {
            if (getTotalCount() == 0) {
                mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_EMPTY);
            } else {
                mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_SUCCESS);
            }
            notifyDataSetChanged();
        }

    }

    /**
     * 增加状态视图，如果增加的视图的{@link StatusView#getStatus()}与默认增加的相同，则会覆盖之前默认的配置
     *
     * @param statusView 状态视图
     */
    public void addStatusView(StatusView statusView) {
        mStatusCell.addStatusView(statusView);
    }

    /**
     * 通知视图为空
     */
    public final void notifyEmpty() {
        if (getTotalCount() == 0) {
            mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_EMPTY);
            if (mLoadMoreCell != null) {
                mLoadMoreCell.setEnable(false);
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 更新状态
     *
     * @param status adapter status
     */
    @CallSuper
    public void notifyStatus(int status) {
        if (status == ViewConstant.VIEW_STATUS_SUCCESS) {
            notifySuccess();
        } else if (status == ViewConstant.VIEW_STATUS_EMPTY) {
            notifyEmpty();
        } else if (status == ViewConstant.VIEW_STATUS_NETWORK_FAILURE) {
            notifyFailure();
        } else {
            if (getTotalCount() == 0) {
                mStatusCell.notifyStatus(status);
                if (mLoadMoreCell != null) {
                    mLoadMoreCell.setEnable(false);
                    mLoadMoreCell.init();
                }
                notifyDataSetChanged();
            }
        }
    }

    /**
     * 是否开启加载更多（若需要,请在初始化adapter开启）
     */
    public void openLoadMore() {
        if (mLoadMoreCell == null) {
            mLoadMoreCell = new DefaultLoadMoreCell();
            setLoadMoreCell(mLoadMoreCell);
        }
    }


    /**
     * 获取当前页数
     *
     * @return
     */
    public int getPage() {
        if (mLoadMoreCell != null) {
            return mLoadMoreCell.getPage();
        }
        return -1;
    }

    /**
     * 初始化数据调用此方法
     */
    public void initPage() {
        if (mLoadMoreCell != null) {
            mLoadMoreCell.init();
        }
    }

    public void setLoadMoreEnable(boolean enable) {
        if (mLoadMoreCell != null) {
            mLoadMoreCell.setEnable(enable);
        }
    }

    /**
     * 装载数据时需要预处理
     */
    public void prepare() {
        if (mLoadMoreCell != null) {
            mStatusCell.notifyStatus(ViewConstant.VIEW_STATUS_SUCCESS);
            mLoadMoreCell.prepare();
        }
    }

    public int getPageSize() {
        if (mLoadMoreCell != null) {
            return mLoadMoreCell.getPageSize();
        }
        return -1;
    }

    /**
     * 上拉监听
     *
     * @param onLoadMoreListener
     */
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        if (mLoadMoreCell != null) {
            mLoadMoreCell.setOnLoadMoreListener(onLoadMoreListener);
        }
    }



}
